package com.tsyun.view_project.list.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.makeramen.roundedimageview.RoundedImageView;
import com.tsyun.view_project.R;
import com.tsyun.view_project.list.bean.DynamicBean;

import java.util.List;

public class SampleAdapter1 extends BaseAdapter {
    //列表显示的条目的数量
    private List<DynamicBean> beans;
    private Context context;
    public SampleAdapter1(Context context, List<DynamicBean> beans) {
        this.context  = context;
        this.beans = beans;
//        this.beans = new ArrayList<>();

    }

    public void setBeans(List<DynamicBean> beans) {
        this.beans = beans;
    }

    @Override
    public int getCount() {
        return beans.size();
    }

    //获取每一个对象的实例
    @Override
    public Object getItem(int position) {
        return null;
    }

    //每一项的ID
    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //LayoutInflater 布局加载器
//        View itemView = null;
        ViewHolder viewHolder;
        if(convertView==null){
            convertView = LayoutInflater.from(context).inflate(R.layout.item_sample,parent,false);
            viewHolder = new ViewHolder();
            viewHolder.tvName = convertView.findViewById(R.id.tv_name);
            viewHolder.tvDate = convertView.findViewById(R.id.tv_date);
            viewHolder.ivHead = convertView.findViewById(R.id.iv_head);
            viewHolder.tvContent = convertView.findViewById(R.id.tv_content);
            convertView.setTag(viewHolder);
//            convertView.setTag();
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        DynamicBean db = beans.get(position);
        //给项目中的每一项赋值
        viewHolder.tvName.setText(db.getName());
        Glide.with(context).load(db.getHeadPic()).into(viewHolder.ivHead);
        viewHolder.tvDate.setText(db.getDate());
        viewHolder.tvContent.setText(db.getContent());
        return convertView;
    }

    //创建服用的class类
    class ViewHolder{
        TextView tvName;
        RoundedImageView ivHead;
        TextView tvDate;
        TextView tvContent;
    }
}
